<?php

class UserController extends Zend_Controller_Action
{
    private $_pegawai;
    private $_user;
    
    
    public function preDispatch() {
        $this->_pegawai = new Application_Model_Pegawai();
        $this->_user = new Application_Model_User();
        
    }
    
    public function init()
    {
        $this->view->site = $this->getRequest()->getControllerName();
        
    }

    public function indexAction()
    {
        $data = $this->_user->getAllData();
        $row = $this->_user->countData();
        
        $this->view->user = $data;
        
        if ($row == 0)
          $this->view->notFound = '<div class="notfound">Tidak Ada Data di Halaman Ini</div>';
        else
          $this->view->notFound = '<div class="notfound">Terdapat '.$row.' Data Pada Tabel Ini</div>';
        
    }

    public function addAction()
    {        
        $step = $this->_getParam('step');
        
        $pegawaiSession = new Zend_Session_Namespace('Default');
        
        if ($step == 1){
            $form = new Application_Form_UserForm();
            
            $this->view->formSearch = $form->getSubForm('searchPegawai');
            
            if($this->getRequest()->isPost())
            {   
                if($form->getSubForm('searchPegawai')->isValid($this->_request->getPost()))
                {
                    $form = $form->getSubForm('searchPegawai');
                    $submit = $form->getValue('submit');

                    if($submit == 'Cari') {
                        $nik = $form->getValue('nik');
                        $pegawai = $this->_pegawai->getPegawaiNik($nik);
                        if($pegawai == false)
                        {
                            $form->populate($this->_request->getPost());
                        }
                        else 
                        {   
                            if (!isset($pegawaiSession->pegawai))
                            {
                                $pegawaiSession->pegawai = $pegawai;
                            }
                            
                            $this->_redirect('/user/add/step/2/');
                        }
                    }
                }
            }
        } elseif ($step == 2) {
            if(isset($pegawaiSession->pegawai)) {
                $form = new Application_Form_UserForm();

                $formSearch = $form->getSubForm('searchPegawai');
                $formSearch->removeElement('submit');
                $formSearch->removeElement('ulang');
                $formSearch->getElement('nik')->setAttrib('disabled', true);
                $formSearch->populate($pegawaiSession->pegawai);
                
                $formCreate = $form->getSubForm('createPegawai');
                $formCreate->removeElement('old_password');
                
                $this->view->formSearch= $formSearch;
                $this->view->formCreate= $formCreate;
                
                
                if($this->getRequest()->isPost())
                {   
                    if($formCreate->isValid($this->_request->getPost()))
                    {
                        $username = $formCreate->getValue('username');
                        $password = $formCreate->getValue('password');
                        $repassword = $formCreate->getValue('repassword');
                        $id_hak_akses = $formCreate->getValue('id_hak_akses');
                        $id_pegawai = $pegawaiSession->pegawai['id_pegawai'];
                        
                        if($this->_user->selectUser($username))
                        {
                            $this->view->errorsUsername = "<ul class='errors'><li>Username Sudah Ada</li></ul>";
                        } else {
                            $user = '1';
                        }
                        
                        if($password != $repassword)
                        {
                            $this->view->errors = "<ul class='errors'><li>Password Tidak Sama</li></ul>";
                        } else {
                            $pass = '1';
                        }
                        if (isset($user) and isset($pass)) {
                            if($user == '1' and $pass == '1')
                            {
                                $data =  array(
                                        'username' => $username,
                                        'password' => $password,
                                        'id_hak_akses' => $id_hak_akses,
                                        'id_pegawai' => $id_pegawai
                                    );
                                    $this->_user->userBaru($data);
                                    $pegawaiSession->unsetAll();
                                    $this->_redirect('user/index');
                            }
                        }
                    }
                }
            }
            else
                $this->_redirect('/user/add/step/1/');
        } 
        else {
            $this->_redirect('/user/add/step/1/');
        }
    }

}

